<?php
/**
 * Created by PhpStorm.
 * User: yxb
 * Date: 2016/12/24
 * Time: 16:25
 */
namespace app\index\controller;
use think\Db;
use think\Controller;
use think\Url;
class Loan extends Controller{
    /**
    借贷个人信息
     * @param uid
     * @param assess_token
     */
    public function loan_member($uid = '',$assess_token = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $member = Db::name("members")->where('uid',$uid)->find();
            return format_return(1,$member,"数据获取成功");
        }
    }
     /**
      *申请借贷
      * @param uid 用户id
      * @param assess_token token盐值
      * @param username 姓名
      * @param id_card 身份证
      * @param card_positive 身份证正面
      * @param card_back 身份证背面
      * @param phone 手机号
      * @param code 验证码
      */
    public function loan($uid = '',$assess_token = '',$username = '',$card_positive = '',$id_card = '',$card_back = '',$phone = '',$code = 0){
        header('Access-Control-Allow-Origin:*');
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            //check_code($code,$phone) || $this->error("验证码错误");
            $where = [
                'status' => 0,
                'uid' => $uid,
                'is_delete' => 0
            ];
            $loan = Db::name("loan")->where($where)->find();
            if(!empty($loan)){
                return json_format_return(0,"","已经申请过贷款，请先等待审批再申请");
            }
            $data = [
                'uid' => $uid,
                'username' => $username,
                'id_card' => $id_card,
                'phone' => $phone,
                'timestamp' => time(),
                'card_positive' => $card_positive,
                'card_back' => $card_back
            ];
            $res = Db::name("loan")->insertGetId($data);
            if(!empty($res)){
                return json_format_return(1,$res,"申请成功");
            }else{
                return json_format_return(0,'',"申请失败");
            }
        }
    }

    /**
    借贷金额
     * @param uid
     * @param assess_token
     * @param loan_id 借款id
     * @param price 金额
     */
    public function loan_price($uid = '',$assess_token = '',$price = 0,$loan_id = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $res = Db::name("loan")->where('id',$loan_id)->update(['price'=>$price]);
            if(!empty($res)){
                return format_return(1,$res,"申请成功");
            }else{
                return format_return(0,'',"申请失败");
            }
        }
    }
}